package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.dto.MenuDto;
import com.itheima.ydd.entity.Menu;
import com.itheima.ydd.entity.RoleMenu;
import com.itheima.ydd.service.MenuService;
import com.itheima.ydd.service.RoleMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */
@Slf4j
@RestController
@RequestMapping("sys_menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Autowired
    private RoleMenuService roleMenuService;

    /**
     * 菜单界面展示
     * @return
     */
    @GetMapping("allData")
    public ResultData getAllData(){
        List<Menu> data = menuService.getMenu();
        return ResultData.ok("",data);
    }

    /**
     * 通过id回显要修改数据具体信息
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData getrowInfo(int id){
        //List<Menu> menus = menuService.menuList(id);
        Menu menu = menuService.getById(id);
        return ResultData.ok("",menu);
    }

    /**
     * 新增菜单
     * @param menu
     * @return
     */
    @PostMapping("add")
    @Transactional
    public ResultData add(@RequestBody Menu menu){
        //将菜单存储到菜单表
        menuService.save(menu);
        //将新增菜单和管理员关联权限
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setRoleId("620003c0-e96c-11eb-845c-3d5efdbaf2d4");
        roleMenu.setMenuId(menu.getId()+"");
        boolean save = roleMenuService.save(roleMenu);
        System.out.println(save);

        return ResultData.ok("添加菜单成功","添加成功");
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData delete(String id){
        menuService.removePid(id);
        return ResultData.ok("删除成功","");
    }

    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Menu menu){
        menuService.updateById(menu);
        return ResultData.ok("修改成功","");
    }
}
